package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.TenantCodeList;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.archive.appManager.service.ITenantCateMdataListService;
import cn.yuanqiao.archive.appManager.service.ITenantCodelistService;
import cn.yuanqiao.archive.appManager.service.impl.TenantMdataGroupServiceImpl;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * codelistController
 * 
 * @author yuanqiao
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/archive/codelist")
public class TenantCodelistController extends BaseController
{
    @Autowired
    private ITenantCodelistService tenantCodelistService;
    @Autowired
    private ITenantCateMdataListService tenantCateMdataListService;

    /**
     * 获取件档号存储字段
     * @author syd
     */
    @GetMapping("/getjiancunchuziduan")
    //@Log(title = "获取件档号存储字段", businessType = BusinessType.SELECT)
    public AjaxResult getjiancunchuziduan(@RequestParam("categoryId") String categoryId){
        List<TenantMdataList> getjiancunchuziduan = tenantCodelistService.getjiancunchuziduan(categoryId);
        return success(getjiancunchuziduan);
    }
    /**
     * 获取档号存储字段
     * @author syd
     */
    @GetMapping("/getcodecolumnname")
    public AjaxResult getcodecolumnname(@RequestParam("categoryId") String categoryId,@RequestParam("type") String type){
        List<TenantMdataList> getcodecolumnname = tenantCodelistService.getcodecolumnname(categoryId,"0".equals(type)?0:1);
        return success(getcodecolumnname);
    }
    /**
     * 获取卷内文件下拉
     * @author syd
     */
    @GetMapping("/getjianziduanselect")
    public AjaxResult getjianziduanselect(@RequestParam("categoryId") String categoryId){
        List<TenantMdataList> getjianziduanselect = tenantCodelistService.getjianziduanselect(categoryId);
        return success(getjianziduanselect);
    }
    /**
     * 规则字段位数change    FIELD_TYPE=1 and IS_HIDDEN=0 为 lastselect
     * @author syd
     */
    @GetMapping("/getselect")
    public AjaxResult getselect(@RequestParam("categoryId") String categoryId,@RequestParam("type") String type){
        List<TenantMdataList> getselect = tenantCodelistService.getselect(categoryId,"0".equals(type)?0:1);
        return success(getselect);
    }

    /**
     * 新增排序获取左侧列表
     * @author syd
     */
    @GetMapping("/getpaixuselect")
    public AjaxResult getpaixuselect(@RequestParam("categoryId") String categoryId,@RequestParam("type") String type){
        List<TenantMdataList> getpaixuselect = tenantCodelistService.getpaixuselect(categoryId,"0".equals(type)?0:1);
        return success(getpaixuselect);
    }
    /**
     * 修改排序获取右侧列表
     * @author syd
     * @param id  档号规则的id
     */
    @GetMapping("/getorderright")
    public AjaxResult getorderright(@RequestParam("id") Long id,@RequestParam("type") String type){
        List<Map<String, Object>> getorderright = tenantCodelistService.getorderright(id, type);
        return success(getorderright);
    }
    /**
     * 修改排序获取左侧列表
     * @author syd
     * @param id  档号规则的id
     */
    @GetMapping("/getorderleft")
    public AjaxResult getorderleft(@RequestParam("id") Long id,@RequestParam("categoryId") String categoryId,@RequestParam("type") String type){
        List<TenantMdataList> getorderleft = tenantCodelistService.getorderleft(id, categoryId, "0".equals(type) ? 0 : 1);
        return success(getorderleft);
    }


    /**
     * 查询codelist列表
     */
    //@RequiresPermissionsmissions("archive:codelist:list")
    @GetMapping("/list")
    public TableDataInfo list(TenantCodeList TenantCodeList)
    {
        startPage();
        List<TenantCodeList> list = tenantCodelistService.selectYqdaCodelistModelList(TenantCodeList);
        return getDataTable(list);
    }

    /**
     * 导出codelist列表
     */
    //@RequiresPermissionsmissions("archive:codelist:export")
    @Log(title = "codelist", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantCodeList TenantCodeList)
    {
        List<TenantCodeList> list = tenantCodelistService.selectYqdaCodelistModelList(TenantCodeList);
        ExcelUtil<TenantCodeList> util = new ExcelUtil<TenantCodeList>(TenantCodeList.class);
        util.exportExcel(response, list, "codelist数据");
    }

    /**
     * 获取codelist详细信息
     */
    //@RequiresPermissionsmissions("archive:codelist:query")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(tenantCodelistService.selectYqdaCodelistModelById(Id));
    }

    /**
     * 档案门类档号定义添加明细
     * @author syd
     * @param json
     * @return
     */
    //@RequiresPermissionsmissions("archive:codelist:add")
    @Log(title = "档案门类档号定义添加明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject json)
    {
        return toAjax(tenantCodelistService.insertYqdaCodelistModel(json));
    }

    /**
     * 档案门类档号定义修改明细
     * @author syd
     * @param json
     * @return
     */
    //@RequiresPermissionsmissions("archive:codelist:edit")
    @Log(title = "档案门类档号定义修改明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JSONObject json)
    {
        return toAjax(tenantCodelistService.updateYqdaCodelistModel(json));
    }

    /**
     * 删除codelist
     */
    //@RequiresPermissionsmissions("archive:codelist:remove")
    @Log(title = "codelist", businessType = BusinessType.DELETE)
	@DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(tenantCodelistService.deleteYqdaCodelistModelByIds(Ids));
    }

    /**
     * 档案门类档号定义获取规则明细
     * @author syd
     * @param archivalcodeRuleId
     * @return
     */
    //@RequiresPermissionsmissions("archive:codelist:query")
    @GetMapping(value = "/getdetail/{type}/{archivalcodeRuleId}")
    @Log(title = "档案门类档号定义获取规则明细", businessType = BusinessType.SELECT)
    public AjaxResult getdetail(@PathVariable("type") String type,@PathVariable("archivalcodeRuleId") Long archivalcodeRuleId)
    {
        return success(tenantCodelistService.getdetail(type,archivalcodeRuleId));
    }

    /**
     * 档号查缺
     * @param
     * @return
     */
    @PostMapping("/dhOfCheckForDefects")
    @PreAuthorize("@ss.hasPermi('archive:wjzl:dhOfCheckForDefects')")
    public AjaxResult dhOfCheckForDefects(@RequestBody JSONObject json)
    {
        return success(tenantCodelistService.dhOfCheckForDefects(json));
    }

    @GetMapping("/getNameByColumn")
    public AjaxResult getNameByColumn(@RequestParam("fieldName") String fieldName,
                                      @RequestParam("categoryId") Long categoryId,@RequestParam("type") String type)
    {
        List<Map<String, Object>> nameByColumn = tenantCodelistService.getNameByColumn(fieldName, categoryId, type);
        return success(nameByColumn);
    }
}
